{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "3cceaab5-78fa-4411-9996-4f6ff26385cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9923898-7936-4cc6-9044-aee87238ed6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = pd.Series([1,3,5,np.nan,44,1])\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebe40bc3-59c7-4cb0-aa7a-78feca6439d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "dates = pd.date_range('20250414',periods=6)\n",
    "print(dates)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2cb3f310-d6c1-482b-8d2b-e2f9183ee8b7",
   "metadata": {},
   "source": [
    "**DataFrame**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea14e35b-acb7-481a-a500-aaebb2e7067d",
   "metadata": {},
   "outputs": [],
   "source": [
    "## DataFrame 是二维的数组\n",
    "## 初始化DataFrame\n",
    "#方法1\n",
    "df0 = pd.DataFrame(np.random.randn(6,4),index=dates,columns=['a','b','c','d']) # index 和 columns 分别是行和列的索引\n",
    "print(df0)\n",
    "df1 = pd.DataFrame(np.random.randn(6,4)) # 行和列的默认索引是 0 1 2 ...\n",
    "print(df1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b587e1a-a0fa-4f6d-ae7b-5569850e4934",
   "metadata": {},
   "outputs": [],
   "source": [
    "#方法2,手动输入每一列数据\n",
    "df2 = pd.DataFrame({\n",
    "    'A': [1.,5.,3.,4.],  # 浮点数\n",
    "    'B': pd.Timestamp('20250414'),  # 时间戳\n",
    "    'C': pd.Series(1, index=list(range(4)), dtype='float32'),  # Series对象\n",
    "    'D': np.array([3] * 4, dtype='int32'),  # NumPy数组\n",
    "    'E': pd.Categorical(['test', 'train', 'test', 'train']),  # 分类数据\n",
    "    'F': \"foo\"  # 字符串\n",
    "})\n",
    "\n",
    "# 输出DataFrame\n",
    "print(\"DataFrame内容：\")\n",
    "print(df2)\n",
    "\n",
    "# 输出每一列的数据类型\n",
    "print(\"\\n每一列的数据类型：\")\n",
    "print(df2.dtypes)\n",
    "\n",
    "# 输出索引\n",
    "print(\"\\n索引：\")\n",
    "print(df2.index)\n",
    "\n",
    "# 输出列名\n",
    "print(\"\\n列名：\")\n",
    "print(df2.columns)\n",
    "\n",
    "# 输出描述(只能描述数字类型，其他类型会被忽略)\n",
    "print(\"\\n描述：\")\n",
    "print(df2.describe())\n",
    "\n",
    "# 输出转置\n",
    "print(\"\\n转置：\")\n",
    "print(df2.T)\n",
    "\n",
    "# 排序\n",
    "print(\"\\n索引排序：\")\n",
    "print(df2.sort_index(ascending=False,axis=1)) # ascending 指定顺序或者倒序\n",
    "\n",
    "print(\"\\n数值排序：\")\n",
    "print(df2.sort_values(by='A')) # ascending 指定顺序或者倒序"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ec6a78d-099c-4c86-84d5-9d20d0a0912a",
   "metadata": {},
   "source": [
    "**选择数据**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7f6a348-7275-4eff-aaa8-10a36e9afd41",
   "metadata": {},
   "outputs": [],
   "source": [
    "dates = pd.date_range('20250415',periods=6)\n",
    "df = pd.DataFrame(np.arange(24).reshape((6,4)),index = dates,columns=['A','B','C','D'])\n",
    "print(df)\n",
    "\n",
    "## 输出某一列\n",
    "print(df['A'],'\\n',df.A) # 两种输出基本一致\n",
    "\n",
    "## 选择某些行\n",
    "print('\\n')\n",
    "print(df[0:3],'\\n',df['20250416':'20250418'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a0970be-162d-4bcc-984d-5a909b08766e",
   "metadata": {},
   "outputs": [],
   "source": [
    "## select by label : loc  纯标签筛选\n",
    "print(df.loc['20250419'])\n",
    "print(df.loc[:,['A','B']]) # 选择所有行，选择 A B 列\n",
    "print('\\n')\n",
    "print(df.loc['20250417':,['A','B']]) # 选择2025-04-17后面所有有行，选择 A B 列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c603ed0-866f-410c-a61e-a0e67ad23ef6",
   "metadata": {},
   "outputs": [],
   "source": [
    "## select by position : iloc (index loc) 纯数字筛选\n",
    "print(df.iloc[0:5,1:3])  # 1:5是左闭右开区间\n",
    "print(df.iloc[[1,3,5],1:3])  # 选择1 3 5 行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0d48209-6219-49fe-82e7-ae09a13f8dce",
   "metadata": {},
   "outputs": [],
   "source": [
    "## Boolean indexing\n",
    "print(df[df.A > 8])\n",
    "print(df[df.A < 8])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7aadaa2-d046-4b03-9f42-5695b16f9525",
   "metadata": {},
   "source": [
    "**设置值**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be79ddb6-1936-42c5-949c-87658bd65d94",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.iloc[2,2] = 1111\n",
    "df.loc['20250416','B']= 2222\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac2961e1-e851-4b64-a773-32ab5683ebe3",
   "metadata": {},
   "outputs": [],
   "source": [
    "df[df.A>5] = 0\n",
    "## 添加一列\n",
    "df['F'] = np.nan\n",
    "df['E'] = np.arange(6)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "320a62ca-0482-44b9-aea4-c846746bff8b",
   "metadata": {},
   "source": [
    "**处理丢失数据**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a5980d2-1078-4cb0-acb9-d7a0918fb29b",
   "metadata": {},
   "outputs": [],
   "source": [
    "dates = pd.date_range('20250415',periods=6)\n",
    "df = pd.DataFrame(np.arange(24).reshape((6,4)),index = dates,columns=['A','B','C','D'])\n",
    "df.iloc[0,1] = np.nan\n",
    "df.iloc[1,2] = np.nan\n",
    "print(df,'\\n')\n",
    "print(df.fillna(value=0)) # 将nan的数据更改为 0\n",
    "# print(df.dropna(axis=0,how='any')) # how = {'any','all'} 删除存在 nan 的 行."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5620692c-05d6-4a6b-8fb7-addcdd02d69d",
   "metadata": {},
   "source": [
    "**导入导出数据**\n",
    "\n",
    "Pandas可导入导出的数据格式包括：csv(也可以读TXT文件), excel, hdf, sql, json, msgpack (experimental), html, gbq (experimental), stata, sas, clipboard, pickle。\n",
    "\n",
    "使用方法都是相同的: `pd.read_xxx` 以及 `pd.to_xxx`(如 `pd.read_csv`,`pd.to_csv`)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1803d1c8-ffc1-452d-92fc-ea541f6e14e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "data= pd.read_csv('student.csv') # 会默认加上索引\n",
    "print(data)\n",
    "data.to_pickle('student.pickle')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c28eaca-d908-4b88-819f-694620741113",
   "metadata": {},
   "source": [
    "**合并DataFrame(concatenating)**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "326193cd-10bd-4c1a-8b03-0dead38aca81",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1 = pd.DataFrame(np.ones((3,4))*0,columns=['a','b','c','d'])\n",
    "df2 = pd.DataFrame(np.ones((3,4))*1,columns=['b','c','d','e'])\n",
    "df3 = pd.DataFrame(np.ones((3,4))*2,columns=['a','b','c','d'])\n",
    "## concatenating\n",
    "df = pd.concat([df1,df2,df3],axis=0,ignore_index=True) # ignore_index，重新生成行标签\n",
    "print(df,'\\n')\n",
    "## join.['inner','outer']\n",
    "df = pd.concat([df1,df2],axis = 0,join='inner') # inner 取交集， outer 取并集\n",
    "print(df)\n",
    "df = pd.concat([df1,df2],axis = 0,join='outer') # inner 取交集， outer 取并集\n",
    "print(df)\n",
    "## append , append 默认是axis = 0，也就是往下加数据, 新版本不支持了"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05d9915a-5bcc-4d26-b439-4ae03618fb8c",
   "metadata": {},
   "source": [
    "**merge**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "8537ee10-2c50-41c3-be13-217bfb803624",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key   A   B\n",
      "0  K0  A0  B0\n",
      "1  K1  A1  B1\n",
      "2  K2  A2  B2\n",
      "3  K3  A3  B3\n",
      "  key   C   D\n",
      "0  K0  C0  D0\n",
      "1  K1  C1  D1\n",
      "2  K2  C2  D2\n",
      "3  K3  C3  D3\n",
      "  key   A   B   C   D\n",
      "0  K0  A0  B0  C0  D0\n",
      "1  K1  A1  B1  C1  D1\n",
      "2  K2  A2  B2  C2  D2\n",
      "3  K3  A3  B3  C3  D3\n"
     ]
    }
   ],
   "source": [
    "left = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],\n",
    "                                  'A': ['A0', 'A1', 'A2', 'A3'],\n",
    "                                  'B': ['B0', 'B1', 'B2', 'B3']})\n",
    "right = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],\n",
    "                                    'C': ['C0', 'C1', 'C2', 'C3'],\n",
    "                                    'D': ['D0', 'D1', 'D2', 'D3']})\n",
    "print(left)\n",
    "print(right)\n",
    "res = pd.merge(left,right,on='key')\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "3d9518b4-f779-4a30-9532-640e198adba8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key1 key2   A   B\n",
      "0   K0   K0  A0  B0\n",
      "1   K0   K1  A1  B1\n",
      "2   K1   K0  A2  B2\n",
      "3   K2   K1  A3  B3\n",
      "  key1 key2   C   D\n",
      "0   K0   K0  C0  D0\n",
      "1   K1   K0  C1  D1\n",
      "2   K1   K0  C2  D2\n",
      "3   K2   K0  C3  D3\n",
      "  key1 key2   A   B    C    D\n",
      "0   K0   K0  A0  B0   C0   D0\n",
      "1   K0   K1  A1  B1  NaN  NaN\n",
      "2   K1   K0  A2  B2   C1   D1\n",
      "3   K1   K0  A2  B2   C2   D2\n",
      "4   K2   K1  A3  B3  NaN  NaN\n"
     ]
    }
   ],
   "source": [
    "# consider two keys\n",
    "left = pd.DataFrame({'key1': ['K0', 'K0', 'K1', 'K2'],\n",
    "                             'key2': ['K0', 'K1', 'K0', 'K1'],\n",
    "                             'A': ['A0', 'A1', 'A2', 'A3'],\n",
    "                             'B': ['B0', 'B1', 'B2', 'B3']})\n",
    "right = pd.DataFrame({'key1': ['K0', 'K1', 'K1', 'K2'],\n",
    "                              'key2': ['K0', 'K0', 'K0', 'K0'],\n",
    "                              'C': ['C0', 'C1', 'C2', 'C3'],\n",
    "                              'D': ['D0', 'D1', 'D2', 'D3']})\n",
    "print(left)\n",
    "print(right)\n",
    "res = pd.merge(left, right, on=['key1', 'key2'], how='inner')  # default for how='inner'\n",
    "# how = ['left', 'right', 'outer', 'inner']\n",
    "res = pd.merge(left, right, on=['key1', 'key2'], how='left')\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c34cb7e8-15b1-4092-8b22-77bc82f64c69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
